package cn.iocoder.yudao.module.warehouse.service.warehouse;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.warehouseoperationlog.dal.dataobject.warehouseoperationlog.WarehouseOperationLogDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.warehouse.controller.admin.warehouse.vo.*;
import cn.iocoder.yudao.module.warehouse.dal.dataobject.warehouse.WarehouseDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.warehouse.dal.mysql.warehouse.WarehouseMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.warehouse.enums.ErrorCodeConstants.*;
// 添加正确的import
import cn.iocoder.yudao.module.warehouseoperationlog.dal.mysql.warehouseoperationlog.WarehouseOperationLogMapper;


/**
 * 库房列表 Service 实现类
 *
 * @author wwwhk
 */
@Service
@Validated
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    private WarehouseMapper warehouseMapper;
    @Resource
    private  WarehouseOperationLogMapper warehouseOperationLogMapper;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createWarehouse(WarehouseSaveReqVO createReqVO) {
        // 插入库房
        WarehouseDO warehouse = BeanUtils.toBean(createReqVO, WarehouseDO.class);
        warehouseMapper.insert(warehouse);

        // 记录操作日志 - 新增操作
        WarehouseOperationLogDO operationLog = new WarehouseOperationLogDO();
        operationLog.setWarehouseId(warehouse.getId());
        operationLog.setOperationType(1); // 1-新增
        operationLog.setOperatorId(SecurityFrameworkUtils.getLoginUserId());
        operationLog.setOperationTime(LocalDateTime.now());
        operationLog.setRemark("新增库房：" + warehouse.getWarehouseName());
        operationLog.setCreator(SecurityFrameworkUtils.getLoginUserNickname());
        operationLog.setUpdater(SecurityFrameworkUtils.getLoginUserNickname());
        warehouseOperationLogMapper.insert(operationLog);

        // 返回
        return warehouse.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWarehouse(WarehouseSaveReqVO updateReqVO) {
        // 校验存在
        WarehouseDO warehouse = validateWarehouseExists(updateReqVO.getId());

        // 更新库房
        WarehouseDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseDO.class);
        warehouseMapper.updateById(updateObj);

        // 记录操作日志 - 修改操作
        WarehouseOperationLogDO operationLog = new WarehouseOperationLogDO();
        operationLog.setWarehouseId(warehouse.getId());
        operationLog.setOperationType(2); // 2-修改
        operationLog.setOperatorId(SecurityFrameworkUtils.getLoginUserId());
        operationLog.setOperationTime(LocalDateTime.now());
        operationLog.setRemark("修改库房：" + warehouse.getWarehouseName());
        operationLog.setCreator(warehouse.getCreator());
        operationLog.setUpdater(SecurityFrameworkUtils.getLoginUserNickname());
        warehouseOperationLogMapper.insert(operationLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteWarehouse(Long id) {
        // 校验存在
        WarehouseDO warehouse = validateWarehouseExists(id);

        // 删除库房
        warehouseMapper.deleteById(id);

        // 记录操作日志 - 删除操作
        WarehouseOperationLogDO operationLog = new WarehouseOperationLogDO();
        operationLog.setWarehouseId(id);
        operationLog.setOperationType(3); // 3-删除
        operationLog.setOperatorId(SecurityFrameworkUtils.getLoginUserId());
        operationLog.setOperationTime(LocalDateTime.now());
        operationLog.setRemark("删除库房：" + warehouse.getWarehouseName());
        operationLog.setCreator(warehouse.getCreator());
        operationLog.setUpdater(SecurityFrameworkUtils.getLoginUserNickname());
        warehouseOperationLogMapper.insert(operationLog);


}

    @Override
        public void deleteWarehouseListByIds(List<Long> ids) {
        // 删除
        warehouseMapper.deleteByIds(ids);
        }


    private WarehouseDO validateWarehouseExists(Long id) {
        WarehouseDO warehouse = warehouseMapper.selectById(id);
        if (warehouse == null) {
            throw exception(WAREHOUSE_NOT_EXISTS);
        }
        return warehouse;
    }

    @Override
    public WarehouseDO getWarehouse(Long id) {
        return warehouseMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseDO> getWarehousePage(WarehousePageReqVO pageReqVO) {
        return warehouseMapper.selectPage(pageReqVO);
    }

}